<template>
  <div>
    <AddOrUpdateSaleModal
      :visible="isShowSaleModal"
      :updateId="updateId"
      :currentItem="currentItem"
      :onHandleOk="onAfterCloseModal"
      :onAfterClose="onAfterCloseModal"
      v-bind="$attrs"
    ></AddOrUpdateSaleModal>
    <a-drawer closable width="60%" title="满减活动列表" placement="right" :visible="visible" @close="onClose">
      <a-button type="primary" style="margin-bottom: 20px" icon="diff" @click="onEditItem('')">添加满减活动</a-button>
      <s-table ref="tableList" size="default" row-key="id" :columns="columns" :data="loadData">
        <template slot="action" slot-scope="text, row">
          <a-button size="small" type="link" style="margin-right: 10px" @click="onEditItem(row.id,row)">修改</a-button>
          <a-button size="small" type="link" @click="delItem(row.id)">删除</a-button>
        </template>
      </s-table>
    </a-drawer>
  </div>
</template>

<script>
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
    width: 80,
  },
  {
    title: '商品类型',
    dataIndex: 'cname',
    key: 'cname',
    width: 100,
  },
  {
    title: '商品名称',
    dataIndex: 'name',
    key: 'name',
    ellipsis: true,
    width: 200,
  },
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status',
    width: 100,
  },
  {
    title: '满减数量',
    dataIndex: 'full_minus_num',
    key: 'full_minus_num',
    width: 100,
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 150,
    scopedSlots: { customRender: 'action' },
  },
]

import AddOrUpdateSaleModal from './AddOrUpdateSaleModal'
import { STable } from '@/components'
import { list, del } from '@/api/goods/full_minus'
export default {
  name: 'CSVUploadModal',
  components: {
    STable,
    AddOrUpdateSaleModal,
  },
  props: {
    goodsId: {
      type: String,
      default: '',
    },
    visible: {
      type: Boolean,
      default: false,
    },
    onAfterClose: {
      type: Function,
      default: () => ({}),
    },
  },
  data() {
    return {
      currentItem:{},
      columns,
      isShowSaleModal: false,
      updateId: '',
      queryParam: {},
      loadData: (parameter) => {
        var newObj = {}
        Object.assign(newObj, parameter, this.queryParam, { id: this.goodsId })
        return list(newObj).then((res) => {
          res.data.data = res.data.list
          return res.data
        })
      },
    }
  },
  watch: {
    goodsId(val) {
      this.$nextTick(() => {
        this.$refs.tableList.$data.localDataSource = []
        this.$refs.tableList.refresh(true)
      })
    },
  },
  created() {},
  mounted() {},
  methods: {
    onEditItem(id,row) {
      if(row){
        this.currentItem = row
      }
      this.updateId = id
      this.isShowSaleModal = true
    },
    onAfterCloseModal(val) {
      if (val === true) {
        this.$nextTick(() => {
          this.$refs.tableList.$data.localDataSource = []
          this.$refs.tableList.refresh(true)
        })
      }
      this.isShowSaleModal = false
      this.updateId = ''
    },
    delItem(id) {
      let that = this
      this.$confirm({
        title: '是否删除该条数据?',
        onOk() {
          del({
            id,
          }).then((res) => {
            if (res.code === 0) {
              that.$nextTick(() => {
                that.$refs.tableList.$data.localDataSource = []
                that.$refs.tableList.refresh(false)
              })
            } else {
              that.$message.error(res.msg)
            }
          })
        },
        onCancel() {},
      })
    },
    onClose() {
      this.$emit('update:visible', false)
    },
  },
}
</script>

<style lang="less" scoped>
</style>
